package com.legrand.message.service.impl;

import com.legrand.message.entity.Message;
import com.legrand.message.mapper.MessageMapper;
import com.legrand.message.service.MessageService;
import com.codingapi.tx.annotation.TxTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.listener.SimpleMessageListenerContainer;
import org.springframework.stereotype.Service;

import javax.jms.ConnectionFactory;

/**
 * @author xiah
 * @date 2018/5/14 10:51
 */
@Service
public class MessageServiceImpl implements MessageService {
       @Autowired
       private MessageMapper messageMapper;

       @Override
       @TxTransaction
       public Long addMessage() {
              Message message = new Message();
              message.setMessage("添加一推奥信息");
              messageMapper.save(message);
              return message.getId();
       }

       /*接收activemq消息 生产消费模式*/
       @JmsListener(destination = "first.blood")
       public void activeListener(String text) {
              System.out.println("1号接受者收到-----------" + text + "----------------");
       }

       /*接收activemq消息  生产消费模式*/
       @JmsListener(destination = "first.blood")
       public void activeListener2(String text) {
              System.out.println("2号接受者收到-----------" + text + "----------------");
       }

       /*接收activemq消息 发布订阅模式*/

       /*@Bean
       public JmsListenerContainerFactory<SimpleMessageListenerContainer> jmsListenerContainerFactory
               (ConnectionFactory connectionFactory) {
              SimpleJmsListenerContainerFactory simpleJmsListenerContainerFactory = new
                      SimpleJmsListenerContainerFactory();
              simpleJmsListenerContainerFactory.setConnectionFactory(connectionFactory);
              simpleJmsListenerContainerFactory.setPubSubDomain(true);
              return simpleJmsListenerContainerFactory;
       }*/

       /*@JmsListener(destination = "first.blood", containerFactory = "jmsListenerContainerFactory")
       public void activeListener3(String text) {
              System.out.println("1号订阅者收到-----------" + text + "----------------");
       }*/

       /*接收activemq消息 发布订阅模式*/
       /*@JmsListener(destination = "first.blood", containerFactory = "jmsListenerContainerFactory")
       public void activeListener4(String text) {
              System.out.println("2号订阅者收到-----------" + text + "----------------");
       }*/
}
